package com.eee.machine.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {
    private final static SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");


    public static Date getBeforeDate(int day){
        Date nowDate = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(nowDate);
        calendar.add(Calendar.DAY_OF_MONTH, -day);
        Date dBefore = calendar.getTime();
        String beforeDayStr = simpleDateFormat2.format(dBefore);
        beforeDayStr = beforeDayStr.substring(0,10)+" 00:00:00";
        try {
            dBefore = simpleDateFormat2.parse(beforeDayStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dBefore;
    }

    public static Date getToDate(){
        Date nowDate = new Date();
        String toDay = simpleDateFormat2.format(nowDate);
        toDay = toDay.substring(0,10)+" 00:00:00";
        try {
            nowDate = simpleDateFormat2.parse(toDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return nowDate;
    }


//    public static Date getToDateLast(){
//        Date nowDate = new Date();
//        String toDay = simpleDateFormat2.format(nowDate);
//        toDay = toDay.substring(0,10)+" 23:59:59";
//        try {
//            nowDate = simpleDateFormat2.parse(toDay);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//        return nowDate;
//    }



    public static Date getFirstDayOfThisMonth(){
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return  cal.getTime();
    }

        public static Date getLastDayOfThisMonth(){
            Calendar cal = Calendar.getInstance();
            cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
            cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
            cal.set(Calendar.HOUR_OF_DAY, 24);
            return cal.getTime();
        }

        public static Date getFirstDayOfPreMonth(){
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.MONTH, -1);
            calendar.set(Calendar.DAY_OF_MONTH,1);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            return calendar.getTime();
    }

    public static Date getLastDayOfPreMonth(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public static String getFirstDayOfMonth(int year,int month){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR,year);
        calendar.set(Calendar.MONTH,month-1);
        calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return sdf.format(calendar.getTime());
    }

    public static String getLastDayOfMonth(int year,int month){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR,year);
        calendar.set(Calendar.MONTH,month-1);
        calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return sdf.format(calendar.getTime());
    }

    public static List<String> getContinueDate(Date startDate,Date endDate){
        List<String> dateList = new ArrayList<>();
        Calendar calBegin  = Calendar.getInstance();
        calBegin .setTime(startDate);
        Calendar calEnd = Calendar.getInstance();
        calEnd.setTime(endDate);
        dateList.add(sdf.format(calBegin.getTime()));
        while (endDate.after(calBegin.getTime()))  {
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            dateList.add(sdf.format(calBegin.getTime()));
        }
        return dateList;
    }


    /**
     * 得到几天前的时间
     * @param d
     * @param day
     * @return
     */
    public static Date getDateBefore(int day){
        Calendar now =Calendar.getInstance();
        now.set(Calendar.DATE,now.get(Calendar.DATE)-day);
        return now.getTime();
    }

    /**
     * 得到指定时间几天后的时间
     * @param day
     * @return
     */
    public static Date getDateAfter(Date date, int day){
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.add(Calendar.DAY_OF_MONTH,day);
        return now.getTime();
    }
    /**
     * LocalDateTime转Date
     * @param localDateTime
     * @return
     */
    public static Date localDateTime2Date(LocalDateTime localDateTime){
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
        Date date = Date.from(zonedDateTime.toInstant());
        return date;
    }

    /**
     * Date转LocalDateTime
     * @param date
     * @return
     */
    public static LocalDateTime Date2LocalDateTime(Date date){
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        return localDateTime;
    }

    /**
     * 判断两个Date之间相差多少
     * @param date1
     * @param date2
     * @return
     */
    public static Long betweenDateAndDate(Date date1, Date date2){
        Calendar instance = Calendar.getInstance();
        instance.setTime(date1);
        long time1 = instance.getTime().getTime();
        instance.setTime(date2);
        long time2 = instance.getTime().getTime();
        return time2-time1 > 0 ? time2-time1 : time1-time2;
    }

    /**
     * 获取指定日期多少小时后的时间
     * @param date
     * @param hours
     * @return
     */
    public static Date afterHourDate(Date date, int hours){
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        instance.add(Calendar.HOUR_OF_DAY, hours);
        Date time = instance.getTime();
        return time;
    }
}
